package cn.iocoder.yudao.module.blog.service.postcomment;

import cn.iocoder.yudao.module.blog.dal.dataobject.spacecomment.BlogSpaceCommentDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.postcomment.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.postcomment.BlogPostCommentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.postcomment.BlogPostCommentConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.postcomment.BlogPostCommentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 帖子评论 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogPostCommentServiceImpl implements BlogPostCommentService {

    @Resource
    private BlogPostCommentMapper postCommentMapper;

    @Override
    public Long createPostComment(BlogPostCommentCreateReqVO createReqVO) {
        // 插入
        BlogPostCommentDO postComment = BlogPostCommentConvert.INSTANCE.convert(createReqVO);
        postCommentMapper.insert(postComment);
        // 返回
        return postComment.getId();
    }

    @Override
    public void updatePostComment(BlogPostCommentUpdateReqVO updateReqVO) {
        // 校验存在
        validatePostCommentExists(updateReqVO.getId());
        // 更新
        BlogPostCommentDO updateObj = BlogPostCommentConvert.INSTANCE.convert(updateReqVO);
        postCommentMapper.updateById(updateObj);
    }

    @Override
    public void deletePostComment(Long id) {
        // 校验存在
        validatePostCommentExists(id);
        // 删除
        postCommentMapper.deleteById(id);
    }

    private void validatePostCommentExists(Long id) {
        if (postCommentMapper.selectById(id) == null) {
            throw exception(POST_COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public BlogPostCommentDO getPostComment(Long id) {
        return postCommentMapper.selectById(id);
    }

    @Override
    public List<BlogPostCommentDO> getPostCommentList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return postCommentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogPostCommentDO> getPostCommentPage(BlogPostCommentPageReqVO pageReqVO) {
        return postCommentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogPostCommentDO> getPostCommentList(BlogPostCommentExportReqVO exportReqVO) {
        return postCommentMapper.selectList(exportReqVO);
    }

    @Override
    public Long getChildCount(Long commentId){
        return postCommentMapper.selectCount(BlogPostCommentDO::getMainCommentId, commentId);
    }

}
